package com.shuqi.activity.viewport;

import com.shuqi.controller.R;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 倒计时TextView
 * 
 * @author
 * 
 */
public class CountDownTimmerView extends TextView {

    private TimeCount mTimeCount;

    public CountDownTimmerView(Context context) {
        super(context);
        initData();
    }

    public CountDownTimmerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    public CountDownTimmerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initData();
    }

    private void initData() {
        mTimeCount = new TimeCount(this, 60000, 1000);
    }

    public void start() {
        mTimeCount.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mTimeCount != null) {
            mTimeCount.cancel();
        }
    }

    class TimeCount extends CountDownTimer {
        private TextView mTextView;

        public TimeCount(TextView textView, long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            mTextView = textView;
        }

        @Override
        public void onFinish() {
            if (mTextView != null) {
                mTextView.setText("重新获取");
                mTextView.setBackgroundResource(R.drawable.identify_code);
                mTextView.setTextColor(android.graphics.Color.parseColor("#14be7d"));
                mTextView.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mTextView != null) {
                mTextView.setClickable(false);
                mTextView.setBackgroundResource(R.drawable.identify_code_n);
                mTextView.setTextColor(android.graphics.Color.parseColor("#b9b9b9"));
                mTextView.setText("重新获取 (" + millisUntilFinished / 1000 + ")");
            }
        }
    }
}
